<template>
  <div class="organzation" ref="organzationContent">
    <p class="title">组织架构</p>
    <Tree
      ref="organizationTree"
      :data="organList"
      :render="renderContent"
      @on-select-change="treeChange"
      class="organization-tree"></Tree>
  </div>
</template>
<script>
  import toolMgr from '@/biz/base/tool.js'
  export default {
    props: {},
    data() {
      return {
        orgId: null,
        organList: []
      }
    },
    async mounted() {
      await this.getOrganzation()
      if (!this.organList.length) {
        return false
      }
      // 默认选中第一条
      this.resolveDefault()
    },
    methods: {
      resolveDefault() {
        this.$set(this.organList[0], 'selected', true)
        this.$set(this.organList[0], 'expand', true)
        this.$emit('onSearch', this.organList[0].orgId)
      },
      // 获取组织架构数据
      getOrganzation() {
        // 获取二级树
        return toolMgr
          .organization(2)
          .then((res) => {
            this.organList = res
          })
          .catch((err) => {
            this.organList = []
          })
      },
      // 自定义当前节点渲染内容
      renderContent(h, { root, node, data }) {
        return h('span', {}, [
          h('span', [
            h('Icon', {
              props: {
                type: 'ios-paper-outline'
              },
              style: {
                marginRight: '8px'
              }
            }),
            h('span', `${data.orgName}`)
          ])
        ])
      },
      treeChange(event) {
        if (!event.length) {
          this.$Message.info('请先选择一个组织架构！')
          this.orgId = null
          this.$emit('onSearch', this.orgId)
          return false
        } else {
          this.orgId = event[0].orgId
          this.$emit('onSearch', this.orgId)
        }
      }
    }
  }
</script>
<style lang="scss" scoped>
  .organzation {
    width: 250px;
    height: calc(100vh - 200px);
    border: 1px solid #ccc;
    margin-right: 20px;
    border-radius: 8px;
    max-height: calc(100% - 60px);
    overflow-y: auto;

    .title {
      font-size: 20px;
      color: #515a6e;
      padding: 10px;
      font-weight: bolder;
    }
    .organization-tree {
      padding: 10px;
    }
  }
</style>
